﻿using System;
using System.Collections.Generic;
using LogSystem;
using UnityEngine;
using UnityEngine.Rendering;
using Yoozoo.Framework.Core;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Gta.Common;
using Yoozoo.HRP.Runtime;
using Yoozoo.Managers.ResourceManagerV2.Runtime;
using Random = UnityEngine.Random;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class StreetEventHittableTargetLine : MonoBehaviour
    {
        // 轨道id
        public int _lineId;
        // 起点
        public Transform _startPoint;
        // 终点
        public Transform _endPoint;
        // 生成点位
        public List<Transform> _spawnPoints = new List<Transform>();
        
        // 当前所有靶子        
        [SerializeField]
        private List<HittableTarget> _targets = new List<HittableTarget>();

        // 轨道长度
        float _lineLength;
        
        private void Awake()
        {
            _lineLength = Vector3.Distance(_startPoint.position, _endPoint.position);
        }

        /// <summary>
        /// 刷新靶子
        /// </summary>
        public void RefreshLine()
        {
            if (!IsLineEmpty())
            {
                return;
            }
            
            // 生成靶子
            ResidentHandler.Inst.GetFunction("CityLuaFunction.refreshHittableTargetLine").Action(StreetEventHittableTargetManager.GetInstance().GroupId, _lineId);
        }
        
        /// <summary>
        /// 添加靶子
        /// </summary>
        /// <param name="target"></param>
        public void AddHittableTarget(HittableTarget target)
        {
            _targets.Add(target);
        }
        
        /// <summary>
        /// 移除靶子
        /// </summary>
        /// <param name="target"></param>
        public void RemoveHittableTarget(HittableTarget target)
        {
            _targets.Remove(target);
        }

        /// <summary>
        /// 随机空闲的生成点
        /// </summary>
        public int RandomEmptySpawnPointIndex()
        {
            List<int> emptySpawnPoints = new List<int>();
            for(int i=1; i<=_spawnPoints.Count; ++i)
            {
                bool isEmpty = true;
                foreach(var target in _targets)
                {
                    if(target.SpawnPointIndex == i)
                    {
                        isEmpty = false;
                        break;
                    }
                }

                if (isEmpty)
                {
                    emptySpawnPoints.Add(i);
                }
            }

            int index = Random.Range(0, emptySpawnPoints.Count);
            return emptySpawnPoints[index];
        }
        
        /// <summary>
        /// 获取生成点位置
        /// </summary>
        /// <param name="index">从1开始</param>
        /// <returns></returns>
        public Vector3 GetSpawnPointPosition(int index)
        {
            return _spawnPoints[index-1].position;
        }

        /// <summary>
        /// 获取轨道上的位置
        /// </summary>
        /// <param name="percent"></param>
        /// <returns></returns>
        public Vector3 GetPointPositionOnLine(float percent)
        {
            return Vector3.Lerp(_startPoint.position, _endPoint.position, percent);
        }

        /// <summary>
        /// 获取轨道长度
        /// </summary>
        /// <returns></returns>
        public float GetLineLength()
        {
            return _lineLength;
        }

        /// <summary>
        /// 判定当前轨道是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsLineEmpty()
        {
            return _targets.Count == 0;
        }
        
        
    }
}